package com.bolt.report.engine.export;

import com.bolt.report.engine.OutputStreamExporterOutput;
import com.bolt.report.exception.ReportException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class SimpleStreamExporterOutput implements OutputStreamExporterOutput {
    private OutputStream outputStream;
    private boolean toClose;


    public SimpleStreamExporterOutput(OutputStream outputStream) {
        this.outputStream = outputStream;
        toClose = false;
    }


    public SimpleStreamExporterOutput(File file) {
        if (file != null) {
            try {
                outputStream = new FileOutputStream(file);
            } catch (IOException e) {
                throw new ReportException(e);
            }
        }
        toClose = true;
    }


    public SimpleStreamExporterOutput(String fileName) {
        this(fileName == null ? null : new File(fileName));
    }


    @Override
    public OutputStream getOutputStream() {
        return outputStream;
    }

    @Override
    public void close() {
        if (toClose) {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
        }
    }
}